home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wmfread.arj / WMFPRINT.C < prev   
C/C++ Source or Header  |  1992-11-17  |  9KB  |  326 lines

  1. /***********************************************************************
  2.  
  3.   MODULE     : WMFPRINT.C
  4.  
  5.   FUNCTIONS  : PrintWMF
  6.                GetPrinterDC
  7.                AbortProc
  8.                AbortDlg
  9.  
  10.   COMMENTS   :
  11.  
  12. ************************************************************************/
  13. // COPYRIGHT:
  14. //
  15. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  16. //
  17. //   You have a royalty-free right to use, modify, reproduce and
  18. //   distribute the Sample Files (and/or any modified version) in
  19. //   any way you find useful, provided that you agree that
  20. //   Microsoft has no warranty obligations or liability for any
  21. //   Sample Application Files which are modified.
  22. #include "windows.h"
  23. #include "wmfdcode.h"
  24.  
  25. /***********************************************************************
  26.  
  27.   FUNCTION   : PrintWMF
  28.  
  29.   PARAMETERS : void
  30.  
  31.   PURPOSE    : draw the metafile on a printer dc
  32.  
  33.   CALLS      : WINDOWS
  34.                  wsprintf
  35.                  MessageBox
  36.                  MakeProcInstance
  37.                  Escape
  38.                  CreateDialog
  39.                  SetMapMode
  40.                  SetViewportOrg
  41.                  SetViewportExt
  42.                  EnableWindow
  43.                  PlayMetaFile
  44.                  DestroyWindow
  45.                  DeleteDC
  46.  
  47.                APP
  48.                  WaitCursor
  49.                  GetPrinterDC
  50.                  SetPlaceableExts
  51.                  SetClipMetaExts
  52.  
  53.   MESSAGES   : none
  54.  
  55.   RETURNS    : BOOL - 0 if unable to print 1 if successful
  56.  
  57.   COMMENTS   :
  58.  
  59.   HISTORY    : 1/16/91 - created - drc - modification of code originally
  60.                contained in SDK sample app PRNTFILE
  61.  
  62. ************************************************************************/
  63.  
  64.  
  65. BOOL PrintWMF ()
  66. {
  67.    char str[50];
  68.  
  69.    /* display the hourglass cursor */
  70.  
  71.    WaitCursor(TRUE);
  72.  
  73.    /* get a DC for the printer */
  74.    hPr = GetPrinterDC();
  75.  
  76.    /* if a DC could not be created then report the error and return */
  77.    if (!hPr)
  78.    {
  79.       WaitCursor(FALSE);
  80.       wsprintf((LPSTR)str, "Cannot print %s", (LPSTR)fnameext);
  81.       MessageBox(hWndMain, (LPSTR)str, NULL, MB_OK | MB_ICONHAND);
  82.       return (FALSE);
  83.    }
  84.  
  85.    /* get a proc address for the abort dialog and procedure */
  86.    lpAbortDlg = MakeProcInstance(AbortDlg, hInst);
  87.    lpAbortProc = MakeProcInstance(AbortProc, hInst);
  88.  
  89.    /* define the abort function */
  90.    Escape(hPr, SETABORTPROC, NULL, (LPSTR)(long)lpAbortProc, (LPSTR)NULL);
  91.  
  92.    /* start the print job */
  93.    if (Escape(hPr, STARTDOC, 4, "Metafile", (LPSTR)NULL) < 0)
  94.    {
  95.       MessageBox(hWndMain, "Unable to start print job", NULL, MB_OK |
  96.                  MB_ICONHAND);
  97.       FreeProcInstance(lpAbortDlg);
  98.       FreeProcInstance(lpAbortProc);
  99.       DeleteDC(hPr);
  100.    }
  101.  
  102.    /* clear the abort flag */
  103.    bAbort = FALSE;
  104.  
  105.    /* Create the Abort dialog box (modeless) */
  106.    hAbortDlgWnd = CreateDialog(hInst, "AbortDlg", hWndMain, lpAbortDlg);
  107.  
  108.    /* if the dialog was not created report the error */
  109.    if (!hAbortDlgWnd)
  110.    {
  111.       WaitCursor(FALSE);
  112.       MessageBox(hWndMain, "NULL Abort window handle", NULL, MB_OK |
  113.                  MB_ICONHAND);
  114.       return (FALSE);
  115.    }
  116.  
  117.    /* show Abort dialog */
  118.    ShowWindow(hAbortDlgWnd, SW_NORMAL);
  119.  
  120.    /* disable the main window to avoid reentrancy problems */
  121.    EnableWindow(hWndMain, FALSE);
  122.    WaitCursor(FALSE);
  123.  
  124.    /* if we are still committed to printing */
  125.    if (!bAbort)
  126.    {
  127.  
  128.       /* if this is a placeable metafile then set its origins and extents */
  129.       if (bAldusMeta)
  130.          SetPlaceableExts(hPr, aldusMFHeader, WMFPRINTER);
  131.  
  132.       /* if this is a metafile contained within a clipboard file then set
  133.          its origins and extents accordingly */
  134.       if ((bMetaInRam) && (!bAldusMeta))
  135.          SetClipMetaExts(hPr, MFP, WMFPRINTER);
  136.  
  137.       /* if this is a "traditional" windows metafile */
  138.       if (!bMetaInRam)
  139.       {
  140.          SetMapMode(hPr, MM_TEXT);
  141.          SetViewportOrg(hPr, 0, 0);
  142.  
  143.          /* set the extents to the driver supplied values for horizontal
  144.             and vertical resolution */
  145.          SetViewportExt(hPr, GetDeviceCaps(hPr, HORZRES), GetDeviceCaps(hPr,
  146.                         VERTRES));
  147.       }
  148.  
  149.       /* play the metafile directly to the printer.  No enumeration involved
  150.          here */
  151.       PlayMetaFile(hPr, hMF);
  152.    }
  153.  
  154.    /* end the print job */
  155.    Escape(hPr, NEWFRAME, 0, 0L, 0L);
  156.    Escape(hPr, ENDDOC, 0, 0L, 0L);
  157.    EnableWindow(hWndMain, TRUE);
  158.  
  159.    /* destroy the Abort dialog box */
  160.    DestroyWindow(hAbortDlgWnd);
  161.    FreeProcInstance(lpAbortDlg);
  162.    FreeProcInstance(lpAbortProc);
  163.    DeleteDC(hPr);
  164.    return (TRUE);
  165. }
  166.  
  167. /***********************************************************************
  168.  
  169.   FUNCTION   : GetPrinterDC
  170.  
  171.   PARAMETERS : void
  172.  
  173.   PURPOSE    : Get hDc for current device on current output port according
  174.                to info in WIN.INI.
  175.  
  176.   CALLS      : WINDOWS
  177.                  GetProfileString
  178.                  AnsiNext
  179.                  CreateDC
  180.  
  181.   MESSAGES   : none
  182.  
  183.   RETURNS    : HANDLE - hDC > 0 if success  hDC = 0 if failure
  184.  
  185.   COMMENTS   : Searches WIN.INI for information about what printer is
  186.                connected, and if found, creates a DC for the printer.
  187.  
  188.   HISTORY    : 1/16/91 - created - originally from the PRNTFILE sample
  189.                supplied with the SDK
  190.  
  191. ************************************************************************/
  192.  
  193.  
  194. HANDLE GetPrinterDC ()
  195. {
  196.    char pPrintInfo[80];
  197.    LPSTR lpTemp;
  198.    LPSTR lpPrintType;
  199.    LPSTR lpPrintDriver;
  200.    LPSTR lpPrintPort;
  201.  
  202.    /* get the current printer from the win.ini */
  203.  
  204.    if (!GetProfileString("windows", "Device", (LPSTR)"", pPrintInfo, 80))
  205.       return (NULL);
  206.    lpTemp = lpPrintType = pPrintInfo;
  207.    lpPrintDriver = lpPrintPort = 0;
  208.    while (*lpTemp)
  209.    {
  210.       if (*lpTemp == ',')
  211.       {
  212.          *lpTemp++ = 0;
  213.          while (*lpTemp == ' ')
  214.             lpTemp = AnsiNext(lpTemp);
  215.          if (!lpPrintDriver)
  216.             lpPrintDriver = lpTemp;
  217.          else
  218.          {
  219.             lpPrintPort = lpTemp;
  220.             break;
  221.          }
  222.       }
  223.       else
  224.          lpTemp = AnsiNext(lpTemp);
  225.    }
  226.  
  227.    /* return a dc for the printer */
  228.    return (CreateDC(lpPrintDriver, lpPrintType, lpPrintPort, (LPSTR)NULL));
  229. }
  230.  
  231. /***********************************************************************
  232.  
  233.   FUNCTION   : AbortProc
  234.  
  235.   PARAMETERS : HDC hPr - printer DC
  236.                int Code - printing status
  237.  
  238.   PURPOSE    : process messages for the abort dialog box
  239.  
  240.   CALLS      : WINDOWS
  241.                  PeekMessage
  242.                  IsDialogMessage
  243.                  TranslateMessage
  244.                  DispatchMessage
  245.  
  246.   MESSAGES   : none
  247.  
  248.   RETURNS    : int
  249.  
  250.   COMMENTS   :
  251.  
  252.   HISTORY    : 1/16/91 - created - originally from the SDK sample app
  253.                PRNTFILE
  254.  
  255. ************************************************************************/
  256.  
  257.  
  258. int FAR PASCAL AbortProc (hPr, Code)
  259. HDC hPr;
  260. int Code;
  261. {
  262.    MSG msg;
  263.  
  264.    /* Process messages intended for the abort dialog box */
  265.  
  266.    while (!bAbort && PeekMessage(&msg, NULL, NULL, NULL, TRUE))
  267.       if (!IsDialogMessage(hAbortDlgWnd, &msg))
  268.       {
  269.          TranslateMessage(&msg);
  270.          DispatchMessage(&msg);
  271.       }
  272.  
  273.    /* bAbort is TRUE (return is FALSE) if the user has aborted */
  274.    return (!bAbort);
  275. }
  276.  
  277. /***********************************************************************
  278.  
  279.   FUNCTION   : AbortDlg
  280.  
  281.   PARAMETERS : HWND hDlg;
  282.                unsigned msg;
  283.                WORD wParam;
  284.                LONG lParam;
  285.  
  286.   PURPOSE    : Processes messages for printer abort dialog box
  287.  
  288.   CALLS      : WINDOWS
  289.                  SetFocus
  290.  
  291.   MESSAGES   : WM_INITDIALOG - initialize dialog box
  292.                WM_COMMAND    - Input received
  293.  
  294.   RETURNS    : int
  295.  
  296.   COMMENTS   : This dialog box is created while the program is printing,
  297.                and allows the user to cancel the printing process.
  298.  
  299.   HISTORY    : 1/16/91 - created - originally from the SDK sample app
  300.                PRNTFILE
  301.  
  302. ************************************************************************/
  303.  
  304.  
  305. int FAR PASCAL AbortDlg (hDlg, msg, wParam, lParam)
  306. HWND hDlg;
  307. unsigned msg;
  308. WORD wParam;
  309. LONG lParam;
  310. {
  311.    switch (msg)
  312.       {
  313.  
  314.    /* Watch for Cancel button, RETURN key, ESCAPE key, or SPACE BAR */
  315.    case WM_COMMAND:
  316.       return (bAbort = TRUE);
  317.  
  318.    case WM_INITDIALOG:
  319.  
  320.       /* Set the focus to the Cancel box of the dialog */
  321.       SetFocus(GetDlgItem(hDlg, IDCANCEL));
  322.       return (TRUE);
  323.       }
  324.    return (FALSE);
  325. }
  326.